package com.day12_Thread.ExcutorServiceDemo;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @ClassName: ExecutorTool
 * @Author: zw
 * @Description: 出百万拳，行万里路；
 * @Create 2025/4/6-11:29
 * @Version 1.0
 */
public class ExecutorTool {
    public static void main(String[] args) {
        // 通过线程池工具类：Executors 调用静态方法，直接得到线程池
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        // 1. 处理Callable 任务
        Future<String> f1 = executorService.submit(new MyCallable1(4));
        Future<String> f2 = executorService.submit(new MyCallable1(5));
        Future<String> f3 = executorService.submit(new MyCallable1(6));
        Future<String> f4 = executorService.submit(new MyCallable1(7));
        try {
            System.out.println(f1.get());
            System.out.println(f2.get());
            System.out.println(f3.get());
            System.out.println(f4.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
class MyCallable1 implements Callable<String> {
    private int n;
    public MyCallable1(int n) {
        this.n = n;
    }
    @Override
    public String call() throws Exception {
        int sum = 0;
        String name = Thread.currentThread().getName();
        for (int i = 0; i < n; i++) {
            sum+=i;
        }
        return name + ":子线程输出：" + sum;
    }
}